var SYS		=	{};



var getValue		=	function(key){
	var clazz=GetCurrentClass();
	var classdat = Get("/clazz/name/"+clazz).obj;
	var fields   = classdat.fields;
	var value 	 = {};
	var info     = [];
	for(f in fields){
		if(fields[f].type=="obj"){
			var obj = Ext.getCmp(fields[f].name+"_id");
			value[fields[f].text]={
				id:obj.getValue()
			};
		}else{
			var obj = Ext.getCmp(fields[f].name);
			if(obj!=null)
				value[fields[f].text]=obj.getValue();
			else log(fields[f].name);
		}
	}
	if(key!=null){
		return value[key]; 
	}
	return value;
};
var setValue		=	function(key,value){
	var clazz=GetCurrentClass();
	var classdat = Get("/clazz/name/"+clazz).obj;
	var fields   = classdat.fields;
	var keyname	 = {};
	for(f in fields){
		keyname[fields[f].text]=fields[f].name;
	}
	if(Ext.getCmp(keyname[key])!=null)
	Ext.getCmp(keyname[key]).setValue(value);
};
/**
 * 添加一个实体
 */
var PostItem		=	function(obj,clazz){
	clazz	=	GetCurrentClass(clazz);
	
};

/**
 * 删除查找到一个删除的实体
 */
var DeleteItem		=	function(key,value,clazz){
	clazz	=	GetCurrentClass(clazz);
};

/**
 * 获取一个实体
 */
var GetItem			=	function(key,value,clazz){
		clazz		=	GetCurrentClass(clazz);
	var classdat 	= 	Get("/clazz/name/"+clazz).obj;
	var db   		= 	Get("/"+classdat.sever+"/"+clazz).obj;
	var data  		= 	[];
	var keyname={};
	var is			=	1;
	if(is!=null)
	for(f in classdat.fields){
		keyname[classdat.fields[f].name]=classdat.fields[f].text;
	}
	for(d in db){
		if(classdat.sever=="entity"){
			var info = db[d].info;
			var obj  = {};
			obj.id 	 = db[d].id;
			for(i in info){
				if(is!=null){
					obj[keyname[info[i].key]] = info[i].value;
				}else
				obj[info[i].key] = info[i].value;
			}
			data.push(obj);
		}else{
			data.push(db[d]);
		}
	}
	if(key!=null){
		for(d in data){
			if(data[d][key]==value)return data[d];
		}
	}
	//Ext.Msg.alert("提示","系统错误:找不到"+clazz+"-"+key+":"+value);
	return null;
};
/**
 * 获取一个实体
 */
var GetItemByValue	=	function(clazz){
		clazz		=	GetCurrentClass(clazz);
	var classdat 	= 	Get("/clazz/name/"+clazz).obj;
	var db   		= 	Get("/"+classdat.sever+"/"+clazz).obj;
	var data  		= 	[];
	var keyname={};
	var is			=	1;
	if(is!=null) 
	classdat.fields.sort(function(a,b){
		return a.index-b.index;
	}); 
	for(f in classdat.fields){
		keyname[classdat.fields[f].name]=classdat.fields[f].text;
	}
	for(d in db){
		if(classdat.sever=="entity"){
			var info = db[d].info;
			var obj  = {};
			obj.id 	 = db[d].id;
			for(i in info){
				if(is!=null){
					obj[keyname[info[i].key]] = info[i].value;
				}else
				obj[info[i].key] = info[i].value;
			}
			data.push(obj);
		}else{
			data.push(db[d]);
		}
	}
	for(d in  data){
		var cdk = 1;
		var bc	= 0;
		var dat = data[d];
		//log(keyname)
		for(f in keyname){
			var c = keyname[f];
			if(arguments[cdk]==data[d][c]){
				
			}else{
				bc=1;
				break;
			}
			cdk++;
			if(cdk>arguments.length-1)break;
		} 
		if(bc==0)return data[d]; 
	}
	return null;
};

/**
 * 设置一个实体的值
 */
var SetItem			=	function(key,value,clazz){
	clazz	=	GetCurrentClass(clazz);
};

/**
 * 根据查询到的实体进行修改
 */
var PutItem			=	function(key,value,obj,clazz){
	clazz	=	GetCurrentClass(clazz);
};

/**
 * 分页查询多个实体
 */
var GetItems		=	function(clazz,start,size,query){
	clazz	=	GetCurrentClass(clazz);
	var classdat 	= 	Get("/clazz/name/"+clazz).obj;
	var db   		= 	Get("/"+classdat.sever+"/"+clazz).obj;
	var keyname={};
	var data  = [];
	var is = true;
	if(is!=null)
	for(f in classdat.fields){
		keyname[classdat.fields[f].name]=classdat.fields[f].text;
	}
	for(d in db){
		if(classdat.sever=="entity"){
			var info = db[d].info;
			var obj  = {};
			obj.id 	 = db[d].id;
			for(i in info){
				if(is!=null){
					obj[keyname[info[i].key]] = info[i].value;
				}else
				obj[info[i].key] = info[i].value;
			}
			data.push(obj);
		}else{
			data.push(db[d]);
		}
	} 
	return data;
};

/**
 * 查询实体的总数
 */
var GetItemsCount	=	function(query,clazz){
	clazz	=	GetCurrentClass(clazz);
	var classdat 	= 	Get("/clazz/name/"+clazz).obj;
	var db   		= 	Get("/"+classdat.sever+"/"+clazz).obj;
	return db.length;
};

/**
 * 删除查询到的实体
 */
var DeletItems		=	function(query,clazz){
	clazz	=	GetCurrentClass(clazz);
};

/**
 * 获取实体的字段
 */
var GetKeys			=	function(clazz){
	clazz	=	GetCurrentClass(clazz);
};


/**
 * 获取当前实体类
 */
var GetCurrentClass	=	function(clazz){
	if(clazz!=null&&clazz.length>0){
		return clazz; 
	}
	return SYS.CurrentClass;
}


/**
 * 设置当前实体类
 */
var setCurrentClass	=	function(clazz){
	SYS.CurrentClass	=	clazz;
}

/**
 * 导入规则
 */
var importRule	=	function(strrule){
	clazz	=	GetCurrentClass(clazz);
	_("entity.fun.actionRule")(strrule,clazz);
	//()()
};


